package com.yxh.activity;

import java.util.LinkedHashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.yxh.IBaseCallBack;
import com.yxh.R;
import com.yxh.ZybApplication;
import com.yxh.dto.UserDto;
import com.yxh.service.DbSqliteService;
import com.yxh.service.MainService;
import com.yxh.service.Task;
import com.yxh.util.AppUtils;
import com.yxh.util.ToastUtil;
import com.yxh.view.Loadingdialog;

public abstract class BaseActivity extends FragmentActivity implements
		IBaseCallBack {

	/*** 初始化Activity ***/
	private Loadingdialog dialog;
	protected Toast toast;
	protected Context mContext;
	protected DbSqliteService db;

	// private View parentView;
	// private View popupWindowView;
	// private PopupWindow popWindow;

	@SuppressLint("ShowToast")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		MainService.addActivity(this);
		super.onCreate(savedInstanceState);
		mContext = ZybApplication.getContext();
		db = new DbSqliteService(mContext);
		dialog = new Loadingdialog(this);
		toast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);

		// popupWindowView = LayoutInflater.from(mContext).inflate(
		// R.layout.view_common_progress, null);
		// popupWindowView.findViewById(R.id.progressBar).getBackground()
		// .setAlpha(200);
		// popWindow = new PopupWindow(popupWindowView,
		// ViewGroup.LayoutParams.FILL_PARENT,
		// ViewGroup.LayoutParams.FILL_PARENT);

	}

	@Override
	protected void onResume() {
		super.onResume();
		closeDialog();
	}

	@Override
	protected void onPause() {
		super.onPause();
		closeDialog();
	}

	/** 获取当前用户 **/
	public UserDto getcurrentUser() {
		String uid = db.getConfigItem(DbSqliteService.USER_ID);
		Log.e("luoming uid", uid + "");
		if (!"0".equals(uid))
			return db.selectOneUserDto(uid);
		return null;
	}

	/** 数据访问 **/
	@Override
	public void refresh(Object... param) {

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getData(LinkedHashMap map, int TaskId, int isShowDialog)// 0:loading
	{
		if (1 == isShowDialog) {
			showDialog();
		}
		map.put("device_code", AppUtils.getmDeviceID());
		UserDto user = getcurrentUser();
		if (user != null) {
			map.put("token", getcurrentUser().token);
		}
		Log.e("luoming", map + "");
		Task task = new Task(TaskId, map);
		MainService.newTask(task);

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getData(LinkedHashMap map, int TaskId) {
		showDialog();
		map.put("device_code", AppUtils.getmDeviceID());
		UserDto user = getcurrentUser();
		if (user != null) {
			map.put("token", getcurrentUser().token);
		}
		Log.e("luoming", map + "");
		Task task = new Task(TaskId, map);
		MainService.newTask(task);
	}

	/** 弹出Toast **/
	public void showToast(int msg) {
		ToastUtil.showMessage(mContext, msg, Toast.LENGTH_SHORT);

	}

	public void showToast(String msg) {
		ToastUtil.showMessage(mContext, msg, Toast.LENGTH_SHORT);

	}

	// public void setParentView(View view){
	// parentView = view;
	// }

	private void showDialog() {
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
		// if (popWindow != null && !popWindow.isShowing()) {
		// popWindow.showAtLocation( parentView, Gravity.NO_GRAVITY, 0, 0);
		// }
	}

	public void closeDialog() {
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}
		// if (popWindow != null && popWindow.isShowing()) {
		// popWindow.dismiss();
		// }
	}

	/************* 退出 **************/
	
	public void titleClickBack(View view){    
		super.onBackPressed();
    }   
	
	// 软按键
	public void onclickBack(View paramView) {
		super.onBackPressed();
	}

	// 硬按键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			onBackPressed();
			return false;
		}
		return false;
	}

	public void onBackPressed() {
		if (this instanceof MainActivity) {
			((MainActivity) this).exitBy2Click();
		} else if (this instanceof UserLoginActivity) {
			noAnimationFinsih();
		} else {
			finish();
		}
	}

	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.noanim, R.anim.exit_leftttoight);
	}

	public void noAnimationFinsih() {
		super.finish();
	}

}